<template>

    <div id="example-2">
        <div>{{url}}</div>
        <div>{{code}}</div>
        <button @click="scan">扫码</button>
    </div>
</template>

<script>

    import { galaxy } from './../../galaxy/index.js';
    let toUrl = '/wholesale/goodsinfo/';

    export default {
        data () {
            return {
                url: window.location.href,
                code: ''
            }
        },
        created () {
            let code = galaxy.getUrlParam('code');
            if (code) {
                this.code = code
            }
            let _this = this;
            this.$wechat.ready(function () {
                _this.$wechat.scanQRCode({
                    needResult: 1,
                    desc: 'scanQRCode desc',
                    success: function (res) {
                        setTimeout(function () {
                            _this.search(res)
                        })
                    }
                })
            })
        },
        methods: {
            scan: function () {
                let _this = this;
                this.$wechat.scanQRCode({
                    needResult: 1,
                    desc: 'scanQRCode desc',
                    success: function (res) {
                        setTimeout(function () {
                            _this.search(res)
                        })
                    }
                })
            },
            search: function (arg) {

                if (arg) {

                    let url = arg.resultStr;
                    if (url.indexOf(',') >= -1) {

                        let tempArray = url.split(',');
                        let tempNum = tempArray[1];
                        this.$router.push(toUrl + tempNum)
                    }
                }
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
